package com.sdnu.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.service.entity.ucenter.FeedbackRecord;
import com.sdnu.service.entity.ucenter.User;
import com.sdnu.service.entity.ucenter.vo.FeedBackRecordListVo;
import com.sdnu.service.mapper.FeedbackRecordMapper;
import com.sdnu.service.service.FeedbackRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdnu.service.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhao YunLai
 * @since 2021-12-05
 */
@Service
public class FeedbackRecordServiceImpl extends ServiceImpl<FeedbackRecordMapper, FeedbackRecord> implements FeedbackRecordService {

    @Autowired
    private UserService userService;

    @Override
    public List<FeedBackRecordListVo> getAll() {

        QueryWrapper<FeedbackRecord> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        List<FeedbackRecord> feedbackRecordList = this.list(wrapper);

        List<FeedBackRecordListVo> finalList = new ArrayList<>();
        for(FeedbackRecord item : feedbackRecordList){
            User user = userService.getById(item.getUserId());
            FeedBackRecordListVo listVo = new FeedBackRecordListVo();
            BeanUtils.copyProperties(item,listVo);
            listVo.setName(user.getName())
                    .setAvatar(user.getAvatar())
                    .setClazz(user.getClazz())
                    .setGender(user.getGender());

            finalList.add(listVo);
        }

        return finalList;
    }

    @Override
    public List<FeedBackRecordListVo> pageSelect(Page<FeedbackRecord> page, QueryWrapper<FeedbackRecord> wrapper) {
        this.page(page,wrapper);
        List<FeedbackRecord> records = page.getRecords();

        List<FeedBackRecordListVo> finalList = new ArrayList<>();
        for(FeedbackRecord item : records){
            User user = userService.getById(item.getUserId());
            FeedBackRecordListVo listVo = new FeedBackRecordListVo();
            BeanUtils.copyProperties(item,listVo);
            listVo.setName(user.getName())
                    .setAvatar(user.getAvatar())
                    .setClazz(user.getClazz())
                    .setGender(user.getGender());

            finalList.add(listVo);
        }
        return finalList;
    }

}
